/**
 <p>属性控件</p>
 * xtype = cceafpropertygridpanel

 * @module Ext-ux-cceaf-control
 * @class cceaf.control.PropertyGridPanel
 * @extends Ext.grid.property.Grid
 * @since 1.0
 */
Ext.define('cceaf.control.PropertyGridPanel', {
    extend: 'Ext.grid.property.Grid',
    alias: 'widget.cceafpropertygridpanel',
    
    requires: [
	    'Ext.Ajax.request'
	],
    
	/**
	 * 加载数据的action
	 *
     * @property loadAction 
     * @type String
     * @default null
     */
	loadAction: null,
	
	/**
	 * 加载数据的action的参数
	 *
     * @property loadActionParams 
     * @type String
     * @default null
     */
	loadActionParams: null,
    
	/**
	 * 模块名称，在tree的title上显示
	 *
     * @property moduleName 
     * @type String
     * @default null
     */
	moduleName: "",
	
	/**
	 * 是否存在刷新按钮，刷新等于调用loadAction
	 *
     * @property hasRefresh 
     * @type boolean
     * @default false
     */
	hasRefresh: false,
	
	/**
	 * 是否有关闭按钮
	 *
     * @property hasClose 
     * @type Boolean
     * @default true
     */
    hasClose: true,
    
	/**
	 * 是否有返回按钮
	 *
     * @property hasBack 
     * @type Boolean
     * @default true
     */
    hasBack: false,
    
	/**
	 * 批量保存数据的action
	 *
     * @property saveAction 
     * @type String
     * @default null
     */
	saveAction: null,
	
	/**
	 * 修改属性后保存数据的action
	 *
     * @property changePropertyAction 
     * @type String
     * @default null
     */
	changePropertyAction: null,
	
	/**
	 * 父容器，grid或tree
	 *
     * @property fatherPanel 
     * @type Panel
     * @default null
     */
    fatherPanel: null,
    
    /**
	 * 提交后是否重新载入父对象的stroe
	 *
     * @property storeLoad 
     * @type Boolean
     * @default true
     */
	storeLoad: true,
	
	/**
	 * 默认null出错
	 */
    source: {},
    
    initComponent : function(){
		var config = this;
	    
		var height = config.height;
		var width = config.width;
		if(config.height == null){
			//height = cceaf.util.ExtHelper.getBodyHeight();
			
			height = cceaf.util.ExtHelper.getMainContentHeight();
		}
		if(config.width == null){
			//width = cceaf.util.ExtHelper.getBodyWidth();
			
			width = cceaf.util.ExtHelper.getMainContentWidth();
		}
		
		var title = '';
		if (config.moduleName) {
			title = config.moduleName + '信息';
		}
		
		var operateArea = [],i=0;
		if(config.hasRefresh){
			operateArea[i] = {itemId: 'btnRefresh', text: ' 刷新 ', iconCls: 'page_refreshIcon', pressed: true};
			i++;
			operateArea[i] = '-';
			i++;
		}
		if(config.saveAction){
			operateArea[i] = {itemId: 'btnSave', text: ' 保存 ', iconCls: 'saveIcon', pressed: true};
			i++;
			operateArea[i] = '-';
			i++;
		}
		if (config.hasClose) {
			operateArea[i] = {itemId: 'btnClose', text: '关闭',pressed: true, iconCls: 'closeIcon'};
			i++;
			operateArea[i] = '-';
			i++;
		}
		if (config.hasBack) {
			operateArea[i] = {itemId: 'btnBack', text: '返回',pressed: true, iconCls: 'backIcon'};
			i++;
			operateArea[i] = '-';
			i++;
		}
		
		var operateToolbar = config.operateToolbar;
		if(operateToolbar == null){
			var reOperateToolbar = config.reOperateToolbar;
			if (reOperateToolbar == null) {
				operateToolbar = {
					//dock: 'top', 
					xtype: 'toolbar',
					items: operateArea
				};
			} else {
				operateToolbar = {
					//dock: 'top', 
					xtype: 'toolbar',
					items: reOperateToolbar
				};
			}
		}else {
			//var array = Ext.Array.merge(operateArea,operateToolbar);
			var array = operateArea.concat(operateToolbar);
			operateToolbar = {
				//dock: 'top', 
				xtype: 'toolbar',
				items: array
			};
		}
		var dockedItems = operateToolbar;
		
	    Ext.applyIf(this, {
			title: title,
	        border: true,
	        frame: true,
	        loadMask: {msg:'正在加载数据，请稍侯……'},
	        dockedItems: dockedItems,
	        listeners : {  
	            'afterrender' : function(propertyGrid) {
	            	if (propertyGrid.loadAction) {
	            		Ext.Ajax.request({
	            			url:  propertyGrid.loadAction,
	            			params: propertyGrid.loadActionParams,
	            			success: function(response){
	            				var isProcessSucess = cceaf.util.ExtHelper.processAjaxRequest(response,true);
	            				if(isProcessSucess){
	            					var jsonObject =eval("(" + response.responseText + ")");  
	            					propertyGrid.setSource(jsonObject.data);
	            				}
	            			},
	            			failure: function(response){
	            				cceaf.util.ExtHelper.processAjaxRequest(response,false);
	            			}
	            		});
	            	}
	            },
	            
	            'propertychange': function( source,  recordId,  value,  oldValue,  eOpts ) {
	            	var changePropertyAction = this.changePropertyAction;
	            	if (changePropertyAction) {
	            		this.page_change_property();
	            	}
	            }
	        }
		});
	    
	    if(!config.autoWidth){
			Ext.applyIf(this, {
		        width: width
			});
		}
		if(!config.autoHeight){
			Ext.applyIf(this, {
				height: height
			});
		}
		
		cceaf.control.PropertyGridPanel.superclass.initComponent.call(this);
    },
    
    initEvents: function() {
    	cceaf.control.PropertyGridPanel.superclass.initEvents.call(this);
    	
    	//注册刷新按钮事件
    	var btnRefresh = this.down('#btnRefresh');
    	if (btnRefresh) {
    		btnRefresh.on('click',this.page_refresh);
		}
    	
    	//注册保存按钮事件
    	var btnSave = this.down('#btnSave');
    	if (btnSave) {
			btnSave.on('click',this.page_save);
		}
    	
    	//注册关闭按钮事件
		var btnClose = this.down('#btnClose');
		if (btnClose) {
			btnClose.on('click',this.page_close);
		}
    	
		//注册返回按钮事件
		var btnBack = this.down('#btnBack');
		if (btnBack) {
			btnBack.on('click',this.page_back);
		}
		
    	//注册其他事件
    	this.registerEvent();
    },
    
    /**
     * 刷新
     *
     */
    page_refresh: function(){
    	var propertyGrid = this.up('cceafpropertygridpanel');
    	var loadAction = propertyGrid.loadAction;

    	if (loadAction) {
    		Ext.Ajax.request({
    			url:  loadAction,
    			params: propertyGrid.loadActionParams,
    			success: function(response){
    				var isProcessSucess = cceaf.util.ExtHelper.processAjaxRequest(response,true);
    				if(isProcessSucess){
    					var jsonObject =eval("(" + response.responseText + ")");  
    					propertyGrid.setSource(jsonObject.data);
    				}
    			},
    			failure: function(response){
    				cceaf.util.ExtHelper.processAjaxRequest(response,false);
    			}
    		});
    	}
    },
    
    /**
     * 当其中1个属性变化时，保存对象
     */
     page_change_property: function(){
    	var propertyGrid = this;
     
    	var changePropertyAction = propertyGrid.changePropertyAction;
     	var storeLoad = propertyGrid.storeLoad;
     	
     	var submitParam = {
     		actionURL: changePropertyAction,
 			closePopWin: true,  //保存数据后关闭窗口
 			formLoad: false, //提交数据后不加载form的数据
 			storeLoad: storeLoad //提交后加载列表页面的store
     	};
 		
     	propertyGrid.page_submit(submitParam);
     },
     
    /**
     * 点击保存按钮，保存对象
     */
     page_save: function(){
    	var propertyGrid = this.up('cceafpropertygridpanel');
     
    	var saveAction = propertyGrid.saveAction;
     	var storeLoad = propertyGrid.storeLoad;
     	
     	var submitParam = {
     		actionURL: saveAction,
 			closePopWin: true,  //保存数据后关闭窗口
 			formLoad: false, //提交数据后不加载form的数据
 			storeLoad: storeLoad //提交后加载列表页面的store
     	};
 		
     	propertyGrid.page_submit(submitParam);
     },
     
    /**
     * 页面数据提交
     * <br> submitParam = {
     * <br>   actionURL： '',	//提交的action
     * <br>   closePopWin: true, //保存数据后关闭窗口
     * <br>   formLoad: false, //提交数据后重新载入数据
     * <br>   storeLoad: false //提交后是否加载列表页面的store
     * <br> }
     *
     * @param {attribute} submitParam 提交参数
     * @param {Function} callback 回调函数
     */
    page_submit: function(submitParam, callback){
    	var propertyGrid = this;
    	
    	var closePopWin = true;
    	var formLoad = false;
    	var storeLoad = true;
    	var actionURL;
    	
    	if (submitParam) {
    		if ( Ext.isDefined( submitParam.formSubmit )) {
    			formSubmit = submitParam.formSubmit;
    		}
    		if ( Ext.isDefined( submitParam.actionURL )) {
    			actionURL = submitParam.actionURL;
    		}
    		if ( Ext.isDefined( submitParam.closePopWin )) {
    			closePopWin = submitParam.closePopWin;
    		}
    		if ( Ext.isDefined( submitParam.formLoad )) {
    			formLoad = submitParam.formLoad;
    		}
    		if ( Ext.isDefined( submitParam.storeLoad )) {
    			storeLoad = submitParam.storeLoad;
    		}
    	}
    	
    	//校验其他特殊规则，需要重载
    	if(!propertyGrid.checkData()){
    		return;
    	}
    	
    	var masterJSONString = Ext.JSON.encode(propertyGrid.getSource());
    	
    	Ext.MessageBox.wait('正在处理数据...', '提示'); //进度条等待
      	Ext.Ajax.request({
      		url:  actionURL,
      		params: {
      			masterJSONString: masterJSONString
      		},
      		success: function(response){
      			var isProcessSucess = cceaf.util.ExtHelper.processAjaxRequest(response,true);
   
   				var addAndEditWin = propertyGrid.findParentByType('window');
    			if (addAndEditWin && isProcessSucess && closePopWin) {
					addAndEditWin.close();
    			}  
    			
    			if (storeLoad) { //数据提交后，需要更新查询数据
    				var fatherPanel = propertyGrid.fatherPanel;
    				if (fatherPanel) {
    					var btnQuery = fatherPanel.down('#btnQuery');
        				if(btnQuery) {
        					btnQuery.fireEvent('click');
        				} else {
        					fatherPanel.getStore().load({params:{isquery:true}});
        				}
    				}
				}
    			
    			if (callback) {
    				callback();
    			}
   			},
   			failure: function(response){
   				cceaf.util.ExtHelper.processAjaxRequest(response,false);
   			}
      	});
      	
    },
    
    /**
     * 属性数据的特殊检验，默认不校验
     *
     * @return {Boolean} 校验成功范围true
     */
     checkData: function(){
     	return true;
     },
     
    /**
     * 关闭页面
     *
     */
    page_close: function(){
     	var propertyGrid = this.up('cceafpropertygridpanel');
     	var addAndEditWin = propertyGrid.findParentByType('window');
  
     	if (addAndEditWin) {
     		addAndEditWin.close();
     	}
    },
     
     /**
      * 返回到查询列表
      *
      */
    page_back: function(){
      	var propertyGrid = this.up('cceafpropertygridpanel');
      	var cardPanel = propertyGrid.up('panel');
      	
      	cardPanel.getLayout().setActiveItem(0);
    },
      
    /**
     * 注册其他事件
     */
    registerEvent: function(){
    	
    }

});